from django.contrib import admin
from django.utils.html import format_html

from personal_center.models import BillGroup, SealApply
from administrative.models import Topics, Company
from forum.models import Article
# Register your models here.
from DjangoUeditor.models import UEditorField
class ArticleAdmin(admin.ModelAdmin):
    list_display = ('title', 'content_html',)
    style_fields = {"content_html": "ueditor"}

admin.site.register(Article, ArticleAdmin)
# admin.site.register(Company)


class BillGroupAdmin(admin.ModelAdmin):
    list_display = ['id', 'menu', 'bill', 'type']
    search_fields = ['bill', 'menu']
    filter_horizontal = ('executor',)


class CompanyAdmin(admin.ModelAdmin):
    list_display = ['name', 'abbreviation', 'director_id', 'leaders_id']
    filter_horizontal = ('deputy_director_ids',)
    # raw_id_fields = ("leaders_id", "head_nurse_id", "director_id")


class SealApplyAdmin(admin.ModelAdmin):
    list_display = ['number', 'apply_user', 'is_fast', 'state']
    filter_horizontal = ('current_audit_users',)
    # raw_id_fields = ("leaders_id", "head_nurse_id", "director_id")


class TopicsAdmin(admin.ModelAdmin):
    list_display = ['id', 'number', 'employee', 'type', 'topics_name', 'state', 'create_time', 'custom_date']
    search_fields = ['number', 'topics_name']
    list_editable = ('state',)
    list_per_page = 5
    # autocomplete_fields = ['help_department']
    filter_vertical = ('help_department',)
    date_hierarchy = 'create_time'

    def custom_date(self, obj):
        return format_html(
            '<span style="color: red;">{}</span>',
            obj.create_time.strftime("%Y-%m-%d %H:%M"))

    custom_date.short_description = '创建时间'

    def save_model(self, request, obj, form, change):
        # print('request', request)
        print(obj.state)
        print(form.cleaned_data.get('state', None))
        super().save_model(request, obj, form, change)


admin.site.register(BillGroup, BillGroupAdmin)
admin.site.register(Topics, TopicsAdmin)
admin.site.register(Company, CompanyAdmin)
admin.site.register(SealApply, SealApplyAdmin)
